import random

N = 3600
pBus = 1. / 36
pCar_0 = 36. / 36
pCar_1 = 15. / 36
pCar_2 = 15. / 36
with open("D:/a_desktop/sumo_study/sumo_sduty/buslane.rou.xml", "w") as routes:  # with as相当于try except的另外一种使用
    print("""<routes>
        <vType id="bus" accel="2" decel="2" length="10" maxSpeed="12" guiShape="bus"/>
        <vType id="car" accel="4" decel="4" length="5" maxSpeed="20" guiShape="passenger"/>
        <route id="route" edges="gneE1" />""", file=routes)  # 将车类型和路线文件写到routes
    veh_bus, veh_car_0, veh_car_1, veh_car_2 = 0, 0, 0, 0
    for i in range(N):
        if random.uniform(0, 1) < pBus:  # uniform在0，1之间产生随机数
            print('    <vehicle id="bus_%i" type="bus" route="route" depart="%i" departLane="0" departSpeed="%i" />' % (
                veh_bus, i, random.uniform(8, 12)), file=routes)
            # departLane=0表示在最右侧车道
            veh_bus += 1
        if random.uniform(0, 1) < pCar_0:
            print('    <vehicle id="car_0_%i" type="car" route="route" depart="%i" departLane="0" departSpeed="%i" />' % (
                veh_car_0, i, random.uniform(12, 20)), file=routes)
            veh_car_0 += 1
        if random.uniform(0, 1) < pCar_1:
            print('    <vehicle id="car_1_%i" type="car" route="route" depart="%i" departLane="1" departSpeed="%i" />' % (
                veh_car_1, i, random.uniform(12, 20)), file=routes)
            veh_car_1 += 1
        if random.uniform(0, 1) < pCar_2:
            print('    <vehicle id="car_2_%i" type="car" route="route" depart="%i" departLane="2" departSpeed="%i" />' % (
                veh_car_2, i, random.uniform(12, 20)), file=routes)
            veh_car_2 += 1

    # 生成路线，一共循环3600次,可以控制车辆生成的数量
    print("</routes>", file=routes)